﻿@using lwzlz.Core;
@{
    Layout = "~/Areas/Mall/Views/Shared/_HomeLayout.cshtml";
    var ProvinceList = ViewData["ProvinceList"] as List<Place>;
    var CityList = ViewData["CityList"] as List<Place>;
    var memberLogin = ViewData["WebHeadMember"] as MemberLogin;
}

<link rel="stylesheet" href="~/Areas/Mall/css/head.css">
<link rel="stylesheet" href="~/Areas/Mall/css/login.css">
<div class="reinstall">
    <div class="top">
        <span>收获地址信息</span>
    </div>
    <div class="content">
        <input id="txtId" type="hidden" value="@(Model!=null?Model.Id:"")" />
        <input id="txtMemberId" type="hidden" value="@memberLogin.Id" />
        <div class="item">
            <span>收货人姓名</span>
            <p><input id="txtRealName" type="text" class="input normal important" value="@(Model!=null?Model.RealName:"")"><span class="wrong"><img src="~/Areas/Mall/image/error.png" />请输入收货人姓名</span></p>
        </div>
        <div class="item">
            <span>手机号</span>
            <p><input id="txtMobile" type="text" class="input normal important" value="@(Model!=null?Model.Mobile:"")"><span class="wrong"><img src="~/Areas/Mall/image/error.png" />请输入手机号</span></p>
        </div>
        <div class="item">
            <span>详细地址</span>
            <p><input id="txtSendAddress" type="text" class="input normal important" value="@(Model!=null?Model.SendAddress:"")"><span class="wrong"><img src="~/Areas/Mall/image/error.png" />请输入详细地址</span></p>
        </div>
        <div class="item">
            <span>配送区域</span>
            <p>
                <select id="secProvince">
                    <option value="">请选择省</option>
                    @if (ProvinceList != null && ProvinceList.Count > 0)
                    {
                        foreach (var item in ProvinceList)
                        {
                            <option value="@item.Code" @(Model != null ? (Model.ProvinceCode == item.Code ? "selected='selected'" : "") : "")>@item.Name</option>
                        }
                    }
                </select>
                <select id="secCity">
                    <option value="">请选择市</option>
                    @if (CityList != null && CityList.Count > 0)
                    {
                        foreach (var item in CityList)
                        {
                            <option value="@item.Code" @(Model != null ? (Model.CityCode == item.Code ? "selected='selected'" : "") : "")>@item.Name</option>
                        }
                    }
                </select>
            </p>
            <span class="wrong"><img src="~/Areas/Mall/image/error.png" />请选择配送区域</span>
        </div>
        <div class="item">
            <span>邮编</span>
            <p><input id="txtPostCode" type="text" class="input normal" value="@(Model!=null?Model.PostCode:"")"></p>
        </div>
    </div>
    <div class="btn-wrap" style="position: static;">
        <input type="submit" value="提交保存" class="btn" onclick="save()">
        <input type="button" value="返回上一页" class="btn yellow" onclick="javascript:history.back(-1);">
    </div>
</div>

<script type="text/javascript" src="~/Areas/Mall/js/jquery-1.10.2.min.js"></script>
<script>
    $(".item .important").blur(function () {
        if ($(this).val()) {
            $(this).parent().find('.wrong').hide();
            $(this).removeClass('error')
        } else {
            $(this).parent().find('.wrong').show();
            $(this).addClass('error')
        }
    });

    //省份更改
    $("#secProvince").change(function () {
        var provinceCode = $("#secProvince").val();
        $.ajax({
            type: "POST",
            url: '@Url.Action("GetCityList", "Address")',
            data: {
                "code": provinceCode
            },
            success: function (res) {
                if (res.Success) {
                    $("#secCity").empty();
                    $("#secCity").append(`<option value="">请选择市</option>`);
                    $(res.Data).each(function (index, item) {
                        $("#secCity").append(`<option value=`+ item.Code +`>`+ item.Name +`</option>`);
                });
                } else {
                    alert(res.Message)
                }
            }
        });
    });

    //检测
    function check() {
        var retBool = true;
        //真实姓名
        var realName = $("#txtRealName").val();
        if (realName == undefined || realName == null || realName == "") {
            $("#txtRealName").parent().find('.wrong').show();
            $("#txtRealName").addClass('error');
            retBool = false;
        } else {
            $("#txtRealName").parent().find('.wrong').hide();
            $("#txtRealName").removeClass('error');
        }

        //手机号
        var mobile = $("#txtMobile").val();
        if (mobile == undefined || mobile == null || mobile == "") {
            $("#txtMobile").parent().find('.wrong').show();
            $("#txtMobile").addClass('error');
            retBool = false;
        } else {
            $("#txtMobile").parent().find('.wrong').hide();
            $("#txtMobile").removeClass('error');
        }

        //详细地址
        var sendAddress = $("#txtSendAddress").val();
        if (sendAddress == undefined || sendAddress == null || sendAddress == "") {
            $("#txtSendAddress").parent().find('.wrong').show();
            $("#txtSendAddress").addClass('error');
            retBool = false;
        } else {
            $("#txtSendAddress").parent().find('.wrong').hide();
            $("#txtSendAddress").removeClass('error');
        }

        //配送省份
        var provinceCode = $("#secProvince").val();
        var provinceName = $("#secProvince").find("option:selected").text();
        if (provinceCode == undefined || provinceCode == null || provinceCode == "") {
            $("#secProvince").parent().parent().find('.wrong').show();
            $("#secProvince").addClass('error');
            retBool = false;
        } else {
            $("#secProvince").parent().parent().find('.wrong').hide();
            $("#secProvince").removeClass('error');
        }

        //配送城市
        var cityCode = $("#secCity").val();
        if (cityCode == undefined || cityCode == null || cityCode == "") {
            $("#secCity").parent().parent().find('.wrong').show();
            $("#secCity").addClass('error');
            retBool = false;
        } else {
            $("#secCity").parent().parent().find('.wrong').hide();
            $("#secCity").removeClass('error');
        }

        return retBool;
    }

    //保存
    function save() {
        if (check()) {
            var id = $("#txtId").val();
            var memberId = $("#txtMemberId").val();
            var realName = $("#txtRealName").val();
            var mobile = $("#txtMobile").val();
            var sendAddress = $("#txtSendAddress").val();
            var provinceCode = $("#secProvince").val();
            var provinceName = $("#secProvince").find("option:selected").text();
            var cityCode = $("#secCity").val();
            var cityName = $("#secCity").find("option:selected").text();
            var postCode = $("#txtPostCode").val();

            $.ajax({
                type: "POST",
                url: '@Url.Action("Save", "Address")',
                data: {
                    "Id": id,
                    "MemberId": memberId,
                    "RealName": realName,
                    "Mobile": mobile,
                    "SendAddress": sendAddress,
                    "ProvinceCode": provinceCode,
                    "ProvinceName": provinceName,
                    "CityCode": cityCode,
                    "CityName": cityName,
                    "PostCode": postCode
                },
                success: function (res) {
                    if (res.Success) {
                        window.location.href = res.Data + '?r=' + (new Date()).valueOf();//直接跳转
                    } else {
                        alert(res.Message)
                    }
                }
            });
        }
    }
</script>